package com.agadmin.exception;


import com.agadmin.response.GlobalResponseBody;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;


/*
 * 全局异常处理函数
 *
 *
 * */

@Slf4j
@ControllerAdvice
public class ResponseHandleException {

    @GlobalResponseBody
    @ExceptionHandler(value = ResponseException.class)
    public GlobalResponseException handle(ResponseException e) {
        GlobalResponseException res = new GlobalResponseException();
        String msg = e.getErrorMsg() == null ? "" : e.getErrorMsg();
        res.setError(e.getExceptionEnum().getMsg() + ":" + msg);
        res.setCode(e.getExceptionEnum().getCode());
        return res;
    }

    @GlobalResponseBody
    @ExceptionHandler(value = NoSuchMethodException.class)
    public GlobalResponseException noMethodHandle(ResponseException e) {
        GlobalResponseException res = new GlobalResponseException();
        String msg = e.getErrorMsg() == null ? "" : e.getErrorMsg();
        res.setError(e.getExceptionEnum().getMsg() + ":" + msg);
        res.setCode(e.getExceptionEnum().getCode());
        return res;
    }

    @GlobalResponseBody
    @ExceptionHandler(value = IllegalArgumentException.class)
    public GlobalResponseException argumentValidHandle(IllegalArgumentException e) {
        GlobalResponseException res = new GlobalResponseException();
        String msg = e.getMessage();
        res.setError(msg);
        res.setCode(403);
        return res;
    }

}
